package com.xunwulian.dubbo.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xunwulian.api.dto.BaseResponse;
import com.xunwulian.api.iface.AppAPI;
import com.xunwulian.base.dao.AppDao;
import com.xunwulian.base.entity.AppEntity;
import com.xunwulian.base.entity.Page;
import com.xunwulian.base.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
public class AppApiImpl implements AppAPI {

    @Autowired
    private AppDao appDao;

    public AppApiImpl() {
    }

    @Override
    public BaseResponse getApp(String appId) {

        BaseResponse response = new BaseResponse();
        response.setData(appDao.getById(appId));

        return response;
    }

    @Override
    public List<AppEntity> listPageByAll(Page page) {
        return appDao.listPageByAll(page);
    }

    @Override
    public BaseResponse getAppList() {
        BaseResponse response = new BaseResponse();
        response.setData(appDao.listAll());

        return response;
    }

    @Override
    public List<AppEntity> list(String roleId, String appId) {

        List<AppEntity> list = new ArrayList<>();
        if (UserUtil.isAdmin(roleId)) {
            list = appDao.listAll();
        } else {
            list.add(appDao.getById(appId));
        }

        return list;
    }
}
